package com.zqr.volatiles;

/**
 * @author zqr
 * @since 2022/11/4
 */
// 测试count++的原子性问题
public class CountDemo {
    // 这里用volatile修饰也没用，可能是因为i++变成复合操作没用了？
    private static volatile int i = 0;

    public static void incr() {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        i++;
//        i = i + 1;
    }

    public static void main(String[] args) {
        for (int j = 0; j < 1000; j++) {
            new Thread(CountDemo::incr).start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(i);
    }
}
